package sofia.bean;

import java.io.*;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;

import org.richfaces.event.UploadEvent;
import org.richfaces.model.UploadItem;


/**
 * ************************************************************************************<br/>
 * Realiza operaciones que permite la carga de videos a la aplicacion<br/>
 * ************************************************************************************<br/>
 * @version 1.0
 * @author JAPeTo jeffersonamado@gmail.com 0748836
 */

public class UploadBean{

	private ArrayList<File> files = new ArrayList<File>();
	private int uploadsAvailable = 1;
	private boolean autoUpload = true;
        /**
         * 
         * @return Retorna la cantidad e archivos que se han subido al servidor
         */
        public int getSize() {
		if (getFiles().size()>0){
			return getFiles().size();
		}else
		{
			return 0;
		}
	}

        /**
         * Contructor
         */
        public UploadBean() {
	}

        /**
         *
         * @param stream
         * @param object
         * @throws IOException
         */
        public synchronized void paint(OutputStream stream, Object object) throws IOException {
//		stream.write(getFiles().get((Integer)object).getData());
	}
        /**
         *
         * @param event
         * @throws Exception
         */
        public synchronized void listener(UploadEvent event) throws Exception{
            UploadItem item = event.getUploadItem();
            File f = item.getFile();
            System.out.println(f.getAbsolutePath());
//	    files.add(file);
	    uploadsAvailable--;
	}

        /**
         *
         * @return limpia el buffer del cargador
         */
        public String clearUploadData() {
		files.clear();
		setUploadsAvailable(1);
		return null;
	}

        /**
         *
         * @return retorna el tiempo que se demora subiendo el archivo
         */
        public long getTimeStamp(){
		return System.currentTimeMillis();
	}

        /**
         *
         * @return retorna los archivos subidos
         * @see java.io.File
         */
        public ArrayList<java.io.File> getFiles() {
		return files;
	}

        /**
         *
         * @param files
         */
        public void setFiles(ArrayList<java.io.File> files) {
		this.files = files;
	}

        /**
         *
         * @return retorna la cantidad de upload exitosos
         */
        public int getUploadsAvailable() {
		return uploadsAvailable;
	}

        /**
         *
         * @param uploadsAvailable
         */
        public void setUploadsAvailable(int uploadsAvailable) {
		this.uploadsAvailable = uploadsAvailable;
	}

        /**
         *
         * @return retorna si la carga es automatica o no
         */
        public boolean isAutoUpload() {
		return autoUpload;
	}

        /**
         *
         * @param autoUpload
         */
        public void setAutoUpload(boolean autoUpload) {
		this.autoUpload = autoUpload;
	}

}
